package main

import (
	"flag"
	"fmt"
	"io"
	"net"
	"os"
)

type Client struct {
	ServerIP   string
	ServerPort int
	Name       string
	conn       net.Conn
	flag       int // 当前client的模式
}

func NewClient(serverIP string, serverPort int) *Client {
	// 创建客户端对象
	client := &Client{
		ServerIP:   serverIP,
		ServerPort: serverPort,
		flag:       999,
	}
	// 链接server
	conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", serverIP, serverPort))
	if err != nil {
		fmt.Println("net.Dial error: ", err)
	}
	client.conn = conn

	// 返回对象
	return client
}

// 处理server回应的goroutine，直接显示到标准输出即可
func (client *Client) DealResponse() {
	io.Copy(os.Stdout, client.conn)
	// 相当于下面代码，且永久阻塞监听
	/* for {
		buf := make([]byte, 4096)
		client.conn.Read(buf)
		fmt.Println(string(buf))
	} */
}

func (client *Client) menu() bool {
	var flag int
	fmt.Println("1.公聊模式")
	fmt.Println("2.私聊模式")
	fmt.Println("3.更新用户名")
	fmt.Println("0.退出")
	fmt.Scanln(&flag)
	if flag >= 0 && flag <= 3 {
		// 合法输入
		client.flag = flag
		return true

	} else {
		fmt.Println(">>>>>>请输入合法范围内的数字<<<<<<")
		return false
	}
}

func (client *Client) UpdateName() bool {
	fmt.Println("请输入用户名：")
	fmt.Scanln(&client.Name)
	sendMsg := "rename|" + client.Name + "\n"
	_, err := client.conn.Write([]byte(sendMsg))
	if err != nil {
		fmt.Println("conn.Write error: ", err)
		return false
	}
	return true
}

func (client *Client) PublickCHat() {
	var chatMsg string
	// 提示用户发送消息
	fmt.Println(">>>>>请输入聊天内容，exit退出聊天<<<<<")
	fmt.Scanln(&chatMsg)

	for chatMsg != "exit" {
		// 发送给服务器

		// 消息不为空就发送
		if len(chatMsg) != 0 {
			sendMsg := chatMsg + "\n"
			_, err := client.conn.Write([]byte(sendMsg))
			if err != nil {
				fmt.Println("conn Write error: ", err)
				break
			}
		}

		chatMsg = ""
		fmt.Println(">>>>>请输入聊天内容，exit退出聊天<<<<<")
		fmt.Scanln(&chatMsg)
	}
}

// 查询在线用户
func (client *Client) SelectUser() {
	sendMsg := "who\n"
	_, err := client.conn.Write([]byte(sendMsg))
	if err != nil {
		fmt.Println("conn.Write error: ", err)
		return
	}

}

func (client *Client) PrivateChat() {
	var remoteName string
	var chatMsg string

	client.SelectUser()
	fmt.Println(">>>>>请输入聊天对象【用户名】，exit退出：")
	fmt.Scanln(&remoteName)
	for remoteName != "exit" {
		fmt.Println(">>>>>请输入消息内容，exit退出：")
		fmt.Scanln(&chatMsg)

		for chatMsg != "exit" {
			// 发送给服务器

			// 消息不为空就发送
			if len(chatMsg) != 0 {
				sendMsg := "to|" + remoteName + "|" + chatMsg + "\n\n"
				_, err := client.conn.Write([]byte(sendMsg))
				if err != nil {
					fmt.Println("conn Write error: ", err)
					break
				}
			}

			chatMsg = ""
			fmt.Println(">>>>>请输入聊天内容，exit退出聊天<<<<<")
			fmt.Scanln(&chatMsg)
		}

		client.SelectUser()
		fmt.Println(">>>>>请输入聊天对象【用户名】，exit退出：")
		fmt.Scanln(&remoteName)
	}
}

// 主业务
func (client *Client) Run() {
	for client.flag != 0 {
		for !client.menu() {
		}

		// 根据不同的模式，处理不同的业务
		switch client.flag {
		case 1:
			// 公聊模式
			client.PublickCHat()
			break
		case 2:
			// 私聊模式
			client.PrivateChat()
			break
		case 3:
			// 感谢用户名
			client.UpdateName()
			break
		}

	}
}

var serverIP string
var serverPort int

func init() {
	flag.StringVar(&serverIP, "ip", "localhost", "默认是：127.0.0.1")
	flag.IntVar(&serverPort, "port", 7777, "默认是：7777")
}

func main() {
	// 命令行解析
	flag.Parse()

	client := NewClient(serverIP, serverPort)
	if client == nil {
		fmt.Println("》》》》链接服务器失败")
		return
	}

	// 单独开启一个goroutine，处理server的回执消息
	go client.DealResponse()

	fmt.Println("》》》》链接服务器成功")

	// 启动客户端的业务
	client.Run()

}
